from mardan import models
from mardan.utils.bootstrap import BootStrapModelForm
from django.core.exceptions import ValidationError


class UserModelForm(BootStrapModelForm):
    class Meta:
        model = models.Yonghu
        fields = ['username', 'password','email','idnumber','head','age','gender','head','idnumber','dianhua']

    def clean_email(self):
        email = self.cleaned_data.get('email')
        # 在这里添加你的自定义验证逻辑
        if '@' not in email:
            raise ValidationError("Email must contain an '@' symbol")
        return email

    def clean_dianhua(self):
        dianhua = self.cleaned_data.get('dianhua')
        # 在这里添加你的自定义验证逻辑
        dianhua_str = str(dianhua)  # 先转换为字符串再计算长度
        if not (len(dianhua_str) == 11 ):
            raise ValidationError("手机号必须是11位")
        return dianhua
    def clean_idnumber(self):
        idnumber = self.cleaned_data.get('idnumber')

        # 身份证号应为15位或18位
        idnumber_str = str(idnumber)  # 先转换为字符串再计算长度
        if not (len(idnumber_str) == 15 or len(idnumber_str) == 18):
            raise ValidationError("身份证号长度应为15位或18位")

            # 检查是否仅包含数字和可能的'X'
        if not idnumber_str.isdigit() and not (
                idnumber_str.replace('X', '').isdigit() and idnumber_str.count('X') == 1):
            raise ValidationError("身份证号应仅包含数字和可能的'X'")

            # 如果需要更严格的验证（比如校验位），可以在这里添加逻辑

        return idnumber
class AdminModelForm(BootStrapModelForm):
    class Meta:
        model = models.Admin
        fields = "__all__"


class CategoryModelForm(BootStrapModelForm):
    class Meta:
        model = models.Category
        fields = "__all__"


class GoodsModelForm(BootStrapModelForm):
    class Meta:
        model = models.Goods
        fields = "__all__"


class OrderModelForm(BootStrapModelForm):
    class Meta:
        model = models.Order
        fields = ["status"]
